package Interface.Insumos.Convenio.DePara;

import Interface.Producao.DigitaNumeros;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import sft.Autenticacao.Observacao;
import sft.Logica.Insumos;
import sft.Principal.Principal;

public class jIDePara extends javax.swing.JInternalFrame {

    public static jDDeParaEdit edit;
    public static Principal t;

    public jIDePara(Principal p) {
        this.t = p;
        initComponents();
        listarCombos();
        keyCombobox(jComboBox1);
        keyCombobox(jComboBox2);
        jTCod.setDocument(new DigitaNumeros());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTCod = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jBAdicionar = new javax.swing.JButton();
        jBEditar = new javax.swing.JButton();
        jBExcluir = new javax.swing.JButton();
        jBSair = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        jLabel1.setText("Cadastro de/Para");

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jComboBox1.setEditable(true);
        jComboBox1.setToolTipText("");
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Convenios:");

        jLabel3.setText("Codigo:");

        jTCod.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTCodFocusLost(evt);
            }
        });

        jComboBox2.setEditable(true);

        jLabel4.setText("Conv-SFT");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox2, 0, 222, Short.MAX_VALUE)
                    .addComponent(jTCod))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jComboBox1, jComboBox2, jTCod});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTCod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jBAdicionar.setText("Adicionar");
        jBAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAdicionarActionPerformed(evt);
            }
        });

        jBEditar.setText("Editar");
        jBEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEditarActionPerformed(evt);
            }
        });

        jBExcluir.setText("Excluir");
        jBExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBExcluirActionPerformed(evt);
            }
        });

        jBSair.setText("Sair");
        jBSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSairActionPerformed(evt);
            }
        });

        jButton1.setText("Limpar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jBAdicionar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBExcluir)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jBSair)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBAdicionar)
                    .addComponent(jBEditar)
                    .addComponent(jBExcluir)
                    .addComponent(jBSair)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSairActionPerformed
        // TODO add your handling code here:
        new Principal().DeParaClose();
        dispose();
    }//GEN-LAST:event_jBSairActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:

        for (int i = 0; i < conv.size(); i++) {
            if (conv.get(i).getNome().equals(jComboBox1.getSelectedItem())) {
                jTCod.setText(String.valueOf(conv.get(i).getCod()));
                for (int j = 0; j < mascara.size(); j++) {
                    if (mascara.get(j).getId() == conv.get(i).getFk_conv()) {
                        jComboBox2.setSelectedItem(mascara.get(j).getNome());
                    }
                }
            }
        }
        if (jComboBox1.getSelectedItem() == null) {
            jComboBox1.setSelectedItem(null);
            jComboBox2.setSelectedItem(null);
            jTCod.setText("");
        }

    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jTCodFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTCodFocusLost
        // TODO add your handling code here:
        for (int i = 0; i < conv.size(); i++) {
            if (jTCod.getText().equals(String.valueOf(conv.get(i).getCod()))) {
                jComboBox1.setSelectedItem(conv.get(i).getNome());
                for (int j = 0; j < mascara.size(); j++) {
                    if (conv.get(i).getFk_conv() == mascara.get(j).getId()) {
                        jComboBox2.setSelectedItem(mascara.get(j).getNome());
                    }
                }
            }
        }
        if (jTCod.getText().isEmpty()) {
            jComboBox1.setSelectedItem(null);
            jComboBox2.setSelectedItem(null);
        }

    }//GEN-LAST:event_jTCodFocusLost

    private void jBAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAdicionarActionPerformed
        try {
            Adicionar();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Atenção", "Erro ao Adicionar!", JOptionPane.ERROR_MESSAGE);
            System.out.println("Erro ao Adicionar: " + ex);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Atenção", "Digite um numero valido no campo \n CODIGO", JOptionPane.ERROR);
            System.out.println("Erro Numero: " + e);
        }

    }//GEN-LAST:event_jBAdicionarActionPerformed

    private void jBExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBExcluirActionPerformed
        try {
            // TODO add your handling code here:
            deletar();
        } catch (SQLException ex) {
            Logger.getLogger(jIDePara.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBExcluirActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        listarCombos();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jBEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEditarActionPerformed
        // TODO add your handling code here:

        mostrarEdit();
    }//GEN-LAST:event_jBEditarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAdicionar;
    private javax.swing.JButton jBEditar;
    private javax.swing.JButton jBExcluir;
    private javax.swing.JButton jBSair;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTCod;
    // End of variables declaration//GEN-END:variables

    private static List<Insumos> conv;
    private static List<Insumos> mascara;

    public final void listarCombos() {
        conv = Principal.getConvenios();
        mascara = Principal.getConv();
        jTCod.setText("");
        jComboBox1.removeAllItems();
        jComboBox1.addItem(null);
        for (int i = 0; i < conv.size(); i++) {
            jComboBox1.addItem(conv.get(i).getNome());
        }
        jComboBox2.removeAllItems();
        jComboBox2.addItem(null);
        for (int i = 0; i < mascara.size(); i++) {
            jComboBox2.addItem(mascara.get(i).getNome());
        }
    }

    private void Adicionar() throws SQLException, NumberFormatException {
        // TODO add your handling code here:
        Insumos dp = new Insumos();
        dp.setNome(jComboBox1.getSelectedItem().toString());
        dp.setCod(Integer.valueOf(jTCod.getText()));
        for (int i = 0; i < mascara.size(); i++) {
            if (jComboBox2.getSelectedItem().equals(mascara.get(i).getNome())) {
                dp.setFk_conv(mascara.get(i).getId());
            }
        }
        DeParaDao dao = new DeParaDao();
        dao.Adicionar(dp);
    }

    private void deletar() throws SQLException {
        int jo = JOptionPane.showConfirmDialog(null, "Deseja realmente DESATIVAR o cadastro aperte (SIM)"
                + "\n aperte (NAO) para voltar o menu anterior", "Atenção",
                JOptionPane.YES_NO_OPTION);

        if (jo == 0) {
            Insumos l = buscaObjeto();
            l.setNome("ZZ " + l.getNome());
            new DeParaDao().Editar(l);
            Observacao.getInstance().setAcao("Não DePara " + l.getNome() + " foi desativado do sistema!");
            Observacao.getInstance().gravaOBS();
            Principal.getConvenios();
            listarCombos();
        }
    }

    private Insumos buscaObjeto() {
        for (int i = 0; i < conv.size(); i++) {
            if (conv.get(i).getNome().equals(jComboBox1.getSelectedItem().toString())) {
                return conv.get(i);
            }
        }
        return null;
    }

    private void mostrarEdit() {
        edit = new jDDeParaEdit(this, true);
        edit.atualiza(jTCod.getText(), mascara, jComboBox1.getSelectedItem().toString(), jComboBox2.getSelectedItem().toString());
        edit.setVisible(true);
    }

    void Editar(String cod, String nome, int fkConv) throws SQLException {
        Insumos i = new Insumos();
        i.setId(buscaObjeto().getId());
        i.setCod(Integer.parseInt(cod));
        i.setNome(nome);
        i.setFk_conv(fkConv);
        new DeParaDao().Editar(i);
    }

    private void keyCombobox(final JComboBox jComboBox1) {
        jComboBox1.setEditable(true);
        jComboBox1.getEditor().getEditorComponent().addKeyListener(
                new KeyListener() {
                    @Override
                    public void keyTyped(KeyEvent keyEvent) {
                    }

                    @Override
                    public void keyPressed(KeyEvent keyEvent) {
                    }

                    @Override
                    public void keyReleased(KeyEvent keyEvent) {
                        jComboBox1.getEditor().setItem(jComboBox1.getEditor().getItem().toString().toUpperCase());
                    }
                });
    }
}
